package com.springcloud.zull.interceptor;

import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author wfh
 */
@Component
public class ResponseBeanIntercepter implements HandlerInterceptor {

    public static final String REQUEST_FLAG = "request_flag";

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        if (handler instanceof HandlerMethod) {
            HandlerMethod handlerMethod = (HandlerMethod) handler;
            if (null != handlerMethod.getMethodAnnotation(ReturnResponseBean.class)) {
                request.setAttribute(REQUEST_FLAG, REQUEST_FLAG);
            } else {
                Class<?> beanType = handlerMethod.getBeanType();
                if (null != beanType.getAnnotation(ReturnResponseBean.class)) {
                    request.setAttribute(REQUEST_FLAG, REQUEST_FLAG);

                }
            }
        }
        return true;
    }

}
